package com.netposa.service;

import com.netposa.utils.FTPUtils2;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class ConsumerTest {

    private static final Logger LOG = LoggerFactory.getLogger(ConsumerTest.class);

    private static ExecutorService service = Executors.newFixedThreadPool(4);

    private static Set<String> ipDirSet = new HashSet<>();

    /**
     * 接收字符串
     *
     * @param record
     */
    @KafkaListener(topics = {"test"})
    public void getMessage(ConsumerRecord<String, String> record) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                LOG.info("消费信息" + record.value());
                System.out.println("当前线程:" + Thread.currentThread().getName());
            }
        });
    }


    /**
     * 接收byte数组
     *
     * @param record
     */
    @KafkaListener(topics = {"test_byte"})
    public void getMessageByte(ConsumerRecord<String, byte[]> record) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                LOG.info("消费信息" + new String(record.value()));
                System.out.println("当前线程:" + Thread.currentThread().getName());
            }
        });

    }

    /**
     * 接收图片
     *
     * @param record
     */
    @KafkaListener(topics = {"test_img"})
    public void getMessageImg(ConsumerRecord<String, byte[]> record) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                LOG.info("消费信息" + new String(record.value()));
                byte[] value = record.value();
                byte2File(value, "E:\\face\\", "img1.jpg");
                FileInputStream inputStream = null;
                try {
                    inputStream = new FileInputStream("E:\\face\\img1.jpg");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                String p1 = "192.168.43.122";
                boolean result = false;
                FTPClient ftpClient = null;
                try {
                    ftpClient = FTPUtils2.getFTPClient();
                    if (!ipDirSet.contains(p1)) {
                        boolean b = FTPUtils2.makeDirectory(p1);
                        if (b) {
                            ipDirSet.add(p1);
                        }
                    }
                    ftpClient.changeWorkingDirectory(p1);
                    result = FTPUtils2.uploadFile("test", inputStream);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        ftpClient.changeWorkingDirectory("/");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


    }

    public static void byte2File(byte[] buf, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
